package com.guocheng.modules.login.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.guocheng.entity.AjaxResult;
import com.guocheng.modules.dictionarie.model.Dictionarie;
import com.guocheng.modules.dictionarie.utils.Dictionaries;
import com.guocheng.modules.login.model.po.Dept;
import com.guocheng.modules.login.service.DeptService;
import com.guocheng.modules.login.utils.DeptUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门信息
 */
@Controller
@RequestMapping("/sys/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    //打开页面
    @GetMapping("/index")
    public String index() {
        return "sys/dept/list";
    }

    /**
     * 部门选择页面
     * @return
     */
    @GetMapping("/select")
    public String selectDept(){
        return "sys/dept/dept";
    }

    /**
     * 获取所有部门简单数据
     *
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public AjaxResult list() {
        List<Dept> depts = deptService.selectList();
        return AjaxResult.ok().data(DeptUtils.deptToDTO(depts));
    }

    /**
     * 打开操作页面
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/show")
    public String show(@RequestParam(value = "id", defaultValue = "-1") Integer id, Model model) {
        if (id.intValue() != -1) {
            Dept dept = deptService.selectById(id);
            model.addAttribute("dept", dept);
        }
        //部门权限字典
        model.addAttribute("permissions",Dictionaries.getDictionaries("dept"));
        return "sys/dept/show";
    }

    /**
     * 添加部门提交
     *
     * @param dept
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public AjaxResult save(Dept dept) {
        if (dept.getName() != null && dept.getName().equals("")) {
            return AjaxResult.error("没有填写部门名称");
        }
        if (dept.getPermission() == null) {
            dept.setPermission(1);
        }
        if(dept.getParentId()==null) dept.setParentId(0);
        boolean insert = deptService.insert(dept);
        if (!insert) return AjaxResult.error("添加失败");
        return AjaxResult.ok("添加成功");
    }

    /**
     * 修改提交
     * @param dept
     * @return
     */
    @PutMapping("/edit")
    @ResponseBody
    public AjaxResult edit(Dept dept) {
        if (dept.getId() == null) {
            return AjaxResult.error("没有id");
        }
        boolean insert = deptService.updateById(dept);
        if (!insert) return AjaxResult.error("修改失败");
        return AjaxResult.ok("修改成功");
    }

    /**
     * 删除提交
     * @param ids
     * @return
     */
    @DeleteMapping("/delete")
    @ResponseBody
    public AjaxResult delete(@RequestParam("ids")List<Integer> ids){
        int i = deptService.deleteByIds(ids);
        return AjaxResult.ok("删除成功");
    }
}
